package com.ruoyi.erp.conventionaldata.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.erp.conventionaldata.domain.ScrappingCauseTab;
import com.ruoyi.erp.conventionaldata.mapper.ScrappingCauseTabBaseMapper;
import com.ruoyi.erp.conventionaldata.service.ScrappingCauseTabBaseService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

@Service
public class ScrappingCauseTabBaseServiceImpl  extends ServiceImpl<ScrappingCauseTabBaseMapper, ScrappingCauseTab> implements ScrappingCauseTabBaseService {

    @Autowired
    private ScrappingCauseTabBaseMapper scrappingCauseTabBaseMapper;

    @Override
    public int delete(String id) {
        String[] routeIds = Convert.toStrArray(id);
        int count = 0;
        for (int i = 0;i < routeIds.length;i++){
            QueryWrapper<ScrappingCauseTab> queryWrapper = new QueryWrapper<>();
            ScrappingCauseTab scrappingCauseTab = new ScrappingCauseTab();
            scrappingCauseTab.setRowkey(routeIds[i]);
            //queryWrapper.like(StringUtils.isNotBlank(scrappingCauseTab.getRowkey()),"rowkey",scrappingCauseTab.getRowkey());
            queryWrapper.eq("rowkey",scrappingCauseTab.getRowkey());
            if (scrappingCauseTabBaseMapper.delete(queryWrapper) > 0){
                count++;
            }
        }
        System.out.println(scrappingCauseTabBaseMapper.deleteById("AF797493D6401507E055000000000001"));

        return  count;
    }

    @Override
    public List<ScrappingCauseTab> select(ScrappingCauseTab scrappingCauseTab) {
        if(StringUtils.isNotBlank(scrappingCauseTab.getRejectMessage())|| StringUtils.isNotBlank(scrappingCauseTab.getRejectReason())){
            QueryWrapper<ScrappingCauseTab> queryWrapper = new QueryWrapper<>();
            queryWrapper.like(StringUtils.isNotBlank(scrappingCauseTab.getRejectReason()),"reject_reason",scrappingCauseTab.getRejectReason());
            queryWrapper.like(StringUtils.isNotBlank(scrappingCauseTab.getRejectMessage()),"reject_message",scrappingCauseTab.getRejectMessage());
            return scrappingCauseTabBaseMapper.selectList(queryWrapper);
        }else{
            System.out.println(scrappingCauseTabBaseMapper.selectList(null).toString());
            return scrappingCauseTabBaseMapper.selectList(null);
        }
    }


    @Override
    public ScrappingCauseTab selectById(String id) {

        QueryWrapper<ScrappingCauseTab> queryWrapper = new QueryWrapper<>();
        ScrappingCauseTab scrappingCauseTab = new ScrappingCauseTab();
        scrappingCauseTab.setRowkey(id);
        //queryWrapper.like(StringUtils.isNotBlank(scrappingCauseTab.getRowkey()),"rowkey",scrappingCauseTab.getRowkey());
        queryWrapper.eq("rowkey",scrappingCauseTab.getRowkey());
        return scrappingCauseTabBaseMapper.selectList(queryWrapper).get(0);

    }

    @Override
    public int insert(ScrappingCauseTab scrappingCauseTab) {
        return scrappingCauseTabBaseMapper.insert(scrappingCauseTab);
    }



    @Override
    public int update(ScrappingCauseTab scrappingCauseTab) {
        return scrappingCauseTabBaseMapper.updateById(scrappingCauseTab);
    }





}
